import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import * as API from '../api/http'
import Item from "antd/es/list/Item";

export let initialState={
  shoplist:[],
  activeKey:"1"
}

const todosSlice = createSlice({
  name: "todos",
  initialState,
  reducers: {
    t_getshoplist(state, action) {
     state.shoplist=action.payload
    },
    t_addshoplist(state,action){//添加
      state.shoplist.push(action.payload)
    },
    t_delshoplist(state,action){//删除
      state.shoplist=state.shoplist.filter
      (item=>item.id!==action.payload)
    },
    t_editshoplist(state,action){//修改
      let i=state.shoplist.findIndex(item=>{
        return item.id==action.payload.id
      })
      state.shoplist[i]=action.payload
    }
  },
});

export const actions = todosSlice.actions;
export default todosSlice.reducer;

export let async_getshoplist=createAsyncThunk('async_getshoplist',
    async(_,store)=>{
      let res=await API.http_getshoplist()
      store.dispatch(actions.t_getshoplist(res))
})
//添加
export let async_addshoplist=createAsyncThunk('async_addshoplist',
    async(obj,store)=>{
      let res=await API.http_addshoplist(obj)
      store.dispatch(actions.t_addshoplist(res))
})
//删除
export let async_delshoplist=createAsyncThunk('async_delshoplist',
    async(id,store)=>{
      await API.http_delshoplist(id)
      store.dispatch(actions.t_delshoplist(id))
})
//编辑
export let async_editshoplist=createAsyncThunk('async_editshoplist',
    async(obj,store)=>{
      let res=await API.http_editshoplist(obj)
      store.dispatch(actions.t_editshoplist(res))
    }
)